package com.kova.api.controller;

import com.kova.api.common.Result;
import com.kova.api.exception.CommonException;
import com.kova.api.log.annotation.ApiLog;
import com.kova.api.log.common.ApiLogConst;
import com.kova.api.version.ApiVersion;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


/**
 * 版本控制
 * 测试： curl http://localhost:8080/api/v1/version/update -X POST
 */
@RestController
@RequestMapping("/api/{version}/version")
public class VersionController {

    //版本更新
    @ResponseBody
    @ApiVersion(1)  //接口版本
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiLog(module = ApiLogConst.version,type = ApiLogConst.detailType,desc = ApiLogConst.detailDesc)
    public Result versionUpdate(){

        Map<String, Object> resultParams = new HashMap<String, Object>();
        resultParams.put("pv", "ios");
        resultParams.put("versionName", "v1.0.1");
        resultParams.put("versionCode", "20");
        resultParams.put("downUrl", "http://www.baidu.com");
        resultParams.put("versionDesc", "有新版本更新");
        resultParams.put("type", "1");
        resultParams.put("gap", "0");
        return Result.ok(resultParams);
    }

    @ResponseBody
    @ApiVersion(1)  //接口版本
    @RequestMapping(value = "/test1",method = RequestMethod.GET)
    @ApiLog(module = ApiLogConst.version,type = ApiLogConst.detailType,desc = ApiLogConst.detailDesc)
    public String test() throws Exception {

        throw new Exception("我是测试的");
    }

    @ResponseBody
    @ApiVersion(1)  //接口版本
    @RequestMapping(value = "/test2",method = RequestMethod.GET)
    @ApiLog(module = ApiLogConst.version,type = ApiLogConst.detailType,desc = ApiLogConst.detailDesc)
    public String test2()  {

        throw new CommonException(1000,"自定义异常");
    }

    @ResponseBody
    @ApiVersion(1)  //接口版本
    @RequestMapping(value = "/test3",method = RequestMethod.GET)
    @ApiLog(module = ApiLogConst.version,type = ApiLogConst.detailType,desc = ApiLogConst.detailDesc)
    public String test3()  {

        int i=10/0;
        return "hello";
    }

    @ResponseBody
    @ApiVersion(1)  //接口版本
    @RequestMapping(value = "/test4",method = RequestMethod.POST)
    @ApiLog(module = ApiLogConst.version,type = ApiLogConst.detailType,desc = ApiLogConst.detailDesc)
    public Result test4(@RequestBody Map<String,Object> params)  {

      return Result.ok();
    }

    @ResponseBody
    @ApiVersion(1)  //接口版本
    @RequestMapping(value = "/test5",method = RequestMethod.GET)
    @ApiLog(module = ApiLogConst.version,type = ApiLogConst.detailType,desc = ApiLogConst.detailDesc)
    public Result test5(@RequestBody Map<String,Object> params)  {

        throw new CommonException(1000,"自定义异常test5");
    }
}
